package com.loe.httptest

import android.content.ContentValues
import android.content.Context
import com.loe.http.Link
import com.loe.json_ext.*
import com.loe.ktui.dist
import com.loe.mvp.ext_java.safe
import com.loe.mvp.ext_java.string
import com.loe.mvp.util.dao.DbTable
import org.json.JSONArray
import org.json.JSONObject
import kotlin.concurrent.thread

/**
 * http测试数据库
 *
 * @author 章路顺
 * @since 2022/7/15-11:20
 */
internal object DbHttpTest : DbTable("http_test")
{
    override fun onInit(context: Context)
    {
        go(
            """
                CREATE TABLE $name ( 
                    id     INTEGER(13)  PRIMARY KEY,
                    data    VARCHAR
                );
            """.trimIndent()
        )
    }

    fun insert(bean: HttpTestBean): Long
    {
        bean.id = System.currentTimeMillis()
        val values = ContentValues()
        values.put("id", bean.id)
        values.put("data", bean.toJsonString())
        return super.insert(values)
    }

    fun update(bean: HttpTestBean): Long
    {
        val values = ContentValues()
        values.put("id", bean.id)
        values.put("data", bean.toJsonString())
        return super.update(values, "id=?", "" + bean.id)
    }

    fun delete(id: Long)
    {
        super.delete("id=?", id.string)
    }

    /** 获取列表 */
    fun getList(): ArrayList<HttpTestBean>
    {
        val js = super.query()
        val list = ArrayList<HttpTestBean>()
        js.forEach<JSONObject>
        {
            val id = it.gotLong("id")
            val bean = it.gotJson("data").toBean<HttpTestBean>()
            bean.id = id
            list.add(bean)
        }
        return list
    }
}